<?php /*a:3:{s:43:"D:\work\tp\view\admins\publisher\index.html";i:1652929335;s:18:"common/header.html";i:1652878502;s:18:"common/footer.html";i:1652023572;}*/ ?>


<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>图书馆管理系统</title>
  <!-- 引入 layui.css -->
  <link rel="stylesheet" href="http://127.0.0.1:8000/layui/css/layui.css">
  <link rel="stylesheet" href="http://127.0.0.1:8000/layer/layer.js">
  <!-- 引入 layui.js -->
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <link rel="stylesheet" href="http://127.0.0.1:8000/static/index.css">
  <script src="http://127.0.0.1:8000/layui/layui.js"></script>
  <link href="http://127.0.0.1:8000/bootstrap-3.4.1-dist/css/bootstrap.css" rel="stylesheet">
  <script src="http://127.0.0.1:8000/jquery-3.6.0.min.js"></script>
  <script src="http://127.0.0.1:8000/bootstrap-3.4.1-dist/js/bootstrap.js"></script>
  <script src="http://127.0.0.1:8000/echarts.min.js"></script>
</head>
<body>
<div class="layui-layout layui-layout-admin">
  <div class="layui-header">
    <div class="layui-logo layui-hide-xs layui-bg-black">图书管理系统</div>
    <ul class="layui-nav layui-layout-right">
      <li class="layui-nav-item"><a href="#"><?php echo session("username"); ?>&nbsp;已登录</a></li>
      <li class="layui-nav-item layui-hide layui-show-md-inline-block">
        <a href="javascript:;">
          更多操作
        </a>
        <dl class="layui-nav-child">
          <dd><a href="/signOut">退出登录</a></dd>
        </dl>
      </li>
    </ul>
  </div>

  <div class="layui-side layui-bg-black">
    <div class="layui-side-scroll">
      <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
      <ul class="layui-nav layui-nav-tree" lay-filter="test">
        <li class="layui-nav-item layui-nav-itemed">
          <a class="" href="javascript:;">账号管理</a>
          <dl class="layui-nav-child">
            <dd><a href="/admins/admin">账号列表</a></dd>
          </dl>
        </li>
        <li class="layui-nav-item">
          <a href="javascript:;">权限管理</a>
          <dl class="layui-nav-child">
            <dd><a href="/admins/rights">权限管理</a></dd>
          </dl>
        </li>
        <li class="layui-nav-item">
          <a href="javascript:;">图书管理</a>
          <dl class="layui-nav-child">
            <dd><a href="/admins/booklist">图书列表</a></dd>
            <dd><a href="/admins/category">图书分类</a></dd>
            <dd><a href="/admins/publisher">供应商</a></dd>
          </dl>
        </li>
        <li class="layui-nav-item">
          <a href="javascript:;">借阅管理</a>
          <dl class="layui-nav-child">
            <dd><a href="/admins/lend">超时列表</a></dd>
          </dl>
        </li>
        <li class="layui-nav-item">
          <a href="javascript:;">数据统计</a>
          <dl class="layui-nav-child">
            <dd><a href="/admins/reports">数据报表</a></dd>
          </dl>
        </li>
      </ul>
    </div>
  </div>
<!-- 内容主体区域 -->
<div class="layui-body">
    <!--        <div style="padding: 15px;"><h1 style="font-weight: 700">欢迎界面</h1></div>-->
    <div id="user_container" style="padding: 15px;">
        <!--            面包屑导航-->
        <span class="layui-breadcrumb">
              <a href="#" style="font-weight: 700;color: #000!important;">首页</a>
              <a href="#" style="font-weight: 700;color: #000!important;">图书管理</a>
              <a href="" >供应商</a>
            </span>
        <div class="layui-row layui-col-space15" style="margin-top: 15px">
            <div class="layui-col-md12">
                <div class="layui-panel" style="padding: 20px">
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <input id="searchVal" style="float: left" type="text" placeholder="请输入编号/名称查找" autocomplete="off" class="layui-input">
                            <button id="search" style="float: left;margin-left: -1px;outline: none;" type="button" class="layui-btn  "><i class="layui-icon layui-icon-search"></i></button>
                            <button data-target="#myModal" data-toggle="modal"  id="addBook" type="button" style="border-radius: 5px;width: 105px;cursor:pointer;margin-left: 20px;" class="layui-btn layui-btn-radius layui-btn-normal">添加供应商</button>
                        </div>
                    </div>
                    <table lay-filter="bookListTable"  class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</div>


<!-- 模态框 -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">添加供应商</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <div class="form-group">
                        <label for="inputBookname" class="col-sm-2 control-label">名称:</label>
                        <div class="col-sm-10">
                            <input name="name" type="text" placeholder="请输入出版社名称" class="form-control" id="inputBookname" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPublisher" class="col-sm-2 control-label">地址:</label>
                        <div class="col-sm-10">
                            <input type="text" name="address" class="form-control" id="inputPublisher" placeholder="请输入出版社地址">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputCategory" class="col-sm-2 control-label">电话:</label>
                        <div class="col-sm-10">
                            <input type="text" name="phone" class="form-control" id="inputCategory" placeholder="请输入出版社电话">
                        </div>
                    </div>
                    <input type="hidden" name="id">
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="sureBtn" data-dismiss="modal" class="btn btn-primary">确认</button>
            </div>
        </div>
    </div>
</div>

<!--状态模板字符-->
<script type="text/html" id="currentTableBar">
    <a data-target="#myModal" data-toggle="modal" class="layui-btn layui-btn-normal layui-btn-sm data-count-edit" lay-event="edit"> <i class="layui-icon layui-icon-edit"></i></a>
    <a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon layui-icon-delete"></i></a>
</script>


<script>
    document.querySelector('.layui-nav-itemed').classList.remove('layui-nav-itemed');
    $('a[href$=publisher]').parent()[0].classList.add('layui-this');
    $('a[href$=publisher]').parent().parent().parent()[0].classList.add('layui-nav-itemed');

    var formhorizontal  = document.querySelector('.form-horizontal')
    var sureBtn = document.getElementById('sureBtn');
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        // 顶部搜索逻辑
        var search = document.getElementById('search');
        var searchVal = document.getElementById('searchVal');
        search.addEventListener('click',function () {
            console.log(searchVal.value)
            table.reload('bookTable', {
                where: { //设定异步数据接口的额外参数，任意设
                    name: searchVal.value
                }
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        })




        // 渲染表格
        table.render({
            elem: '#currentTableId',
            limit:4
            ,url: "<?php echo url('/admins/publisher/article'); ?>" //数据接口
            ,page: true //开启分页
            ,limits:[2,4,6,8]
            ,skin: 'row,line'
            ,id:'bookTable'
            ,cols: [[ //表头
                // {type: "checkbox", },
                {field: 'id',align:"center", width: 80, title: 'ID', sort: true},
                {field: 'name',align:"center", title: '名称'},
                {field: 'address', align:"center",title: '地址'},
                {field: 'phone', align:"center",title: '联系电话'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],

        });


        // 添加图书信息表格
        var addBook = document.getElementById('addBook');
        var myModalLabel = document.getElementById('myModalLabel');
        var tableReload = table.reload;


        addBook.addEventListener('click', function (){
            formhorizontal.reset();
            myModalLabel.innerHTML = '添加供应商';
            sureBtn.dataset.sureType = 'add'
        })

        sureBtn.addEventListener('click',async function (e) {
            if(sureBtn.dataset.sureType === 'add') {
                const {data: res} = await axios.post('/admins/publisher/addPublisher', {
                    name : formhorizontal.name.value,
                    address :  formhorizontal.address.value,
                    phone:formhorizontal.phone.value,
                    create_at: new Date().valueOf()
                })
                 // console.log(res)
                if (res.state !== 200) {
                    return layer.msg(res.message, {
                        icon: 0,
                        time: 1000
                    })
                }
                table.reload('bookTable', {});
                layer.msg('添加成功', {
                    icon: 1,
                    time: 1500
                })
            }else{
                const {data:res} = await axios.post('/admins/publisher/edit',{
                    name : formhorizontal.name.value,
                    address :  formhorizontal.address.value,
                    phone:formhorizontal.phone.value,
                    id:parseInt(formhorizontal.id.value),
                    update_at:(new Date()).valueOf()
                })
                if(res.state!==200){
                    return layer.msg(res.message, {
                        icon: 0,
                        time: 1000
                    })}
                table.reload('bookTable', {});
                layer.msg('更改成功', {
                    icon: 1,
                    time: 1500
                })
            }
        })

        // 编辑删除绑定事件
        table.on('tool(bookListTable)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if(layEvent === 'delete'){ //删除
                layer.confirm('该操作将会永久删除该用户是否继续？',{icon: 3, title:'提示'}, async function(index){
                    await axios.get(`/admins/publisher/delete?id=${data.id}`)
                    table.reload('bookTable', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    layer.close(index);
                    //向服务端发送删除指令
                });
            } else if(layEvent === 'edit'){
                //编辑
                //do something
                sureBtn.dataset.sureType = 'edit'
                // console.log(sureBtn.dataset.sureType)
                formhorizontal.name.value = data.name;
                formhorizontal.address.value=data.address;
                formhorizontal.phone.value=data.phone
                myModalLabel.innerHTML = '修改信息';
                formhorizontal.id.value = data.id
            }
        });

    });




</script>



</body>

</html>
